<?php
include_once (SYSTEM.'/Controller.php');
include_once (MODEL . '/ProfileRepository.php');

class ProfileController extends Controller
{
	public function index(){
		$params = $this->GetParams();
		$id= $params['id'];
		if ($id==="") {
			$AccountID = '4fdee32a6803fa610b000000';
			
		}
		else 
		{
			$AccountID=$id;
		}
		$this->ViewData['Title'] = 'User profile';
		$profile = new ProfileRepository();
		
		$this->ViewData['id']= $AccountID;
		$model['form'] = $profile->ViewInform($AccountID);
		$this->Render('index', '_Master',$model);
	}
	public function ViewInform()
	{
		$params = $this->GetParams();
	    $AccountID = '4fdee32a6803fa610b000000';
		$this->ViewData['Title'] = 'User profile';
		$profile = new ProfileRepository();
		
		$this->ViewData['id']= $AccountID;
		$model['form'] = $profile->ViewInform($AccountID);
		$this->Render('viewInform', '_Master',$model);
	}
	
	public function EditName()
	{
		$AccountID= '4fdee32a6803fa610b000000';
		$this->ViewData['Title'] = 'Edit name';
		$profile = new ProfileRepository();
		$params = $this->GetParams();
		if(!isset($params['submit']))
		{
			$model['form'] = $profile->ViewInform($AccountID);
			$this->Render('EditName', '_Master',$model);
		}
		else
		{
			$FirstName = $params['FirstName'];
			$MiddleName =$params['MiddleName'];
			$LastName = $params['LastName'];
			$Sex = $params['Sex'];
			$Birthday = $params['Birthday'];
			$profile->UpdateName($AccountID, $FirstName,$MiddleName,$LastName,$Sex,$Birthday);
			$this->Redirect('/Profile/viewInform');
		}	
	}
	public function EditJob()
	{
		$AccountID= '4fdee32a6803fa610b000000';
		$this->ViewData['Title'] = 'Edit job detail';
		$profile = new ProfileRepository();
		$params = $this->GetParams();
		if(!isset($params['submit']))
		{
			$model['form'] = $profile->ViewInform($AccountID);
			$this->Render('EditJob', '_Master',$model);
		}
		else
		{
			$Occupation = $params['Occupation'];
			$Company = $params['Company'];			
			$profile->UpdateJob($AccountID, $Occupation, $Company);
			$this->Redirect('/Profile/viewInform');
		}
	}
	
	public function EditAddress()
	{
	
		$AccountID= '4fdee32a6803fa610b000000';
		$this->ViewData['Title'] = 'Edit Address';
		$profile = new ProfileRepository();
		$params = $this->GetParams();
		if(!isset($params['submit']))
		{
			$model['form'] = $profile->ViewInform($AccountID);
			$this->Render('EditAddress', '_Master',$model);
		}
		else
		{
			$Street = $params['Street'];
			$City =$params['City'];
			$Country = $params['Country'];
			$profile->UpdateAddress($AccountID, $Street, $City, $Country);
			$this->Redirect('/Profile/viewInform');
		}	
	}
	
	public function ChangePass()
	{
		$AccountID= '4fdee32a6803fa610b000000';
		$this->ViewData['Title'] = 'Change your password';
		$profile = new ProfileRepository();
		$checkPass = new ProfileRepository();
		$passWord  = $checkPass->ViewInform($AccountID)->Password;
		 $params = $this->GetParams();
		 $this->ViewData['error1']="";
		 $this->ViewData['error2']="";
		 
		 $model['form'] = $profile->ViewInform($AccountID);
		if(!isset($params['submit']))
		{
		$this->Render('ChangePass', '_Master',$model);
		}
		else 
		{
			$oldPass = md5($params['oldPassword']);
			$newPass = $params['newPassword'];
			$repeatPW = $params['repeatPW'];
			if ($passWord==$oldPass) {
				if ($newPass==$repeatPW) {
					$profile->ChangePass($AccountID, $newPass);
				    $this->Redirect('/Profile/viewInform');
				}
				else {
					$this->ViewData['error2']= "xac nhan mat khau moi khong dung";
					$this->Render('ChangePass', '_Master',$model);
				}
			}
			else{
				$this->ViewData['error1']= "mat khau ban dau khong chinh xac";
				$this->Render('ChangePass', '_Master',$model);
				
			}
			
		}
	}
	public function EditImage()
	{
		$params = $this->GetParams();   
		$this->ViewData['error']="";
		$imageName =$params['id'];
		$this->ViewData['id']=$imageName;
		if(!isset($params['submit']))
		{
			$this->Render('editImage','_Master');
		}
		else
		{
			if ((($_FILES["file"]["type"] == "image/gif")
					|| ($_FILES["file"]["type"] == "image/jpeg")
					|| ($_FILES["file"]["type"] == "image/pjpeg"))
					&& ($_FILES["file"]["size"] < 1000000))
			{
			
				if ($_FILES["file"]["error"] > 0)
				{
					$this->ViewData['error']= "Error: " . $_FILES["file"]["error"] . "<br />";
					$this->Render('editImage','_Master');
				}
				else{
					$existFile = UPLOAD."/User/Avatar/".$imageName.".jpg";
					if(file_exists($existFile))
					{
						unlink($existFile);
					}
					move_uploaded_file($_FILES["file"]["tmp_name"],$existFile);
					
					$this->Redirect('/Profile/viewInform');
			
				}
			}
			else
			{
				$this->ViewData['error']= "khong dung dinh dang file hoac dung luong qua lon";
				$this->Render('editImage','_Master');
			}
		}
	}
}